import { HashRouter as Router, Routes, Route, Navigate, useLocation } from 'react-router-dom'
import { useEffect } from 'react'
import { ThemeProvider } from '@/contexts/ThemeContext'
import { AIChatProvider } from '@/contexts/aiChat'
import { SettingProvider } from '@/contexts/setting'
import { useBusiness } from '@/stores/useBusinessStore'
import { TemplateProvider } from '@/contexts/template'
import Navbar from '@/components/Navbar'
import { ChatContainer } from '@/components/aiChat'
import { SettingDialog } from '@/components/setting'
import Work from '@/pages/Work'
import AICorrector from '@/pages/aiCorrector'
import BusinessPage from '@/pages/business'
import TemplateImportPage from '@/pages/template'
import DocumentPage from '@/pages/document'
import ProofMapperPage from '@/pages/ProofMapper'
import { MessageTip } from '@/components/common/MessageTip'
import { RemediationProvider } from '@/contexts/RemediationContext'
import { DocumentProvider } from '@/contexts/document'



function App() {
  return (
    <ThemeProvider>
      <SettingProvider>
        <AIChatProvider>
          <BusinessContextConsumer />
        </AIChatProvider>
      </SettingProvider>
    </ThemeProvider>
  )
}

// 内部组件，用于消费BusinessContext
const BusinessContextConsumer = () => {
  const { selectedBusiness, fetchBusinessSystems } = useBusiness();
  
  // 组件挂载时获取业务系统数据
  useEffect(() => {
    fetchBusinessSystems();
  }, [fetchBusinessSystems]);
  
  // 受保护的路由组件，检查是否有选中的业务系统
  const ProtectedRoute = ({ children }: { children: React.ReactNode }) => {
    const location = useLocation();
    
    // 如果是 /template 页面，不受限制
    if (location.pathname === '/template') {
      return <>{children}</>;
    }
    
    // 如果是 /business 页面，不受限制
    if (location.pathname === '/business') {
      return <>{children}</>;
    }
    
    // 其他页面，如果没有选中业务系统，重定向到 /business
    if (!selectedBusiness) {
      return <Navigate to="/business" replace />;
    }
    
    // 有选中的业务系统，正常渲染
    return <>{children}</>;
  };
  
  return (
    <TemplateProvider>
      <RemediationProvider planId={selectedBusiness?.id} businessName={selectedBusiness?.name} assessmentType={selectedBusiness?.assessmentType}>
        <DocumentProvider>
          <Router>
          <div className="min-h-screen bg-background text-foreground transition-colors duration-300">
            <Navbar />
            <main className="container mx-auto px-4 py-8">
              <Routes>
                <Route path="/" element={<Navigate to="/business" replace />} />
                <Route path="/business" element={<BusinessPage />} />
                <Route path="/template" element={<TemplateImportPage />} />
                <Route path="/work" element={
                  <ProtectedRoute>
                    <Work />
                  </ProtectedRoute>
                } />
                <Route path="/corrector" element={
                  <ProtectedRoute>
                    <AICorrector />
                  </ProtectedRoute>
                } />
                <Route path="/document" element={
                  <ProtectedRoute>
                    <DocumentPage />
                  </ProtectedRoute>
                } />
                <Route path="/proof-mapper" element={
                  <ProtectedRoute>
                    <ProofMapperPage />
                  </ProtectedRoute>
                } />
              </Routes>
            </main>
            {/* AI聊天悬浮按钮，在所有页面可见 */}
            <ChatContainer />
            <MessageTip key="toast-container" />
            {/* 系统设置对话框，在所有页面可见 */}
            <SettingDialog />
          </div>
          </Router>
        </DocumentProvider>
      </RemediationProvider>
    </TemplateProvider>
  )
}

export default App
